//
//  ChainNavBarView.m
//  Chain
//
//  Created by zhaosh on 2018/4/3.
//  Copyright © 2018年 zhaosh. All rights reserved.
//
#import "ChainNavBarView.h"
#import "ChainSearchView.h"

@interface ChainNavBarView ()
@property(strong,nonatomic)UIButton *backBtn;
@property(strong,nonatomic)ChainSearchView *searchV;
@property(strong,nonatomic)UILabel *titleV;
@property(strong,nonatomic)UIImageView *msgIcon;
@property(strong,nonatomic)UILabel *redDotV;
@property(strong,nonatomic)UIButton *rightBtn;
@property(weak,nonatomic)  UINavigationController *nav;
@property(assign,nonatomic)FXNavType type;
@property(strong,nonatomic)NSString *placeholderStr;
@property(strong,nonatomic)UIButton *searchBtn;
@end

@implementation ChainNavBarView
- (instancetype)initWithFrame:(CGRect)frame Type:(FXNavType)type placeholderStr:(NSString *)str vc:(UINavigationController *)vc{
    if (self =[super initWithFrame:frame]) {
        self.nav = vc;
        self.type = type;
        self.placeholderStr = str;
        [self setupContents];
    }
    return self;
}

- (void)setupContents{
    if (self.nav.childViewControllers.count > 1) {
        self.backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, kStatusBarHeight, 44, 44)];
        [self.backBtn setImage:[UIImage imageNamed:@"back_icon"] forState:UIControlStateNormal];
        [self.backBtn addTarget:self action:@selector(backBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.backBtn];
    }
    CGFloat startX = 0;
    if (self.backBtn) {
        startX = self.backBtn.right + 7;
    }else{
        startX = 20;
    }
    if (self.type == ChainNavTypeSearch || self.type == ChainNavTypeSearchAndRightOtherButton) {
        self.searchV = [[ChainSearchView alloc]initWithFrame:CGRectMake(startX, kStatusBarHeight, ScreenWidth - startX - 13 - 25 - 20, 44)];
        self.searchV.searchViewType = FXSearchViewTypeTitleV;
        self.searchV.placeholder = self.placeholderStr;
        self.searchV.textFCanEdit = NO;
        [self addSubview:self.searchV];
        
        __weak typeof(self) weakSelf = self;
        self.searchV.titleClickedBlock = ^{
            if (weakSelf.block) {
                weakSelf.block();
            }
        };
    }else if(self.type == ChainNavTypeOnlyTitle){
        self.titleV = [[UILabel alloc]initWithFrame:CGRectMake(startX, kStatusBarHeight, ScreenWidth - 2 * startX, 44)];
        self.titleV.font = FONT_LB(18);
        self.titleV.textColor = [UIColor blackColor];
        self.titleV.textAlignment = NSTextAlignmentCenter;
        self.titleV.text = self.placeholderStr;
        [self addSubview:self.titleV];
    }else if(self.type == ChainNavTypeTitleAndSearch){
        self.titleV = [[UILabel alloc]initWithFrame:CGRectMake(startX, kStatusBarHeight, ScreenWidth - 2 * startX, 44)];
        self.titleV.font = [UIFont boldSystemFontOfSize:18];
        self.titleV.textColor = [UIColor whiteColor];
        self.titleV.textAlignment = NSTextAlignmentCenter;
        self.titleV.text = self.placeholderStr;
        [self addSubview:self.titleV];
    }

    self.rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.width - 60, kStatusBarHeight, 60, 44)];
    self.rightBtn.backgroundColor = [UIColor clearColor];
    [self.rightBtn setTitle:@"搜索" forState:UIControlStateNormal];
    [self addSubview:self.rightBtn];
    
    [self.rightBtn addTarget:self action:@selector(rightBtnClicked) forControlEvents:UIControlEventTouchUpInside];
}

- (void)backBtnClicked{
    [self.nav popViewControllerAnimated:YES];
}

- (void)rightBtnClicked{
    __weak typeof(self) weakSelf = self;
    if (weakSelf.otherButtonBlock) {
        weakSelf.otherButtonBlock();
    }
}

- (void)searchBtnAction{
    if (self.searchBlock) {
        self.searchBlock();
    }
}
@end
